const es_tool = require("./es_ast_tool")
const babel_tool = require("./babel_ast_tool")
const esprima = require("esprima")
const estraverse = require("estraverse")
const escodegen = require("escodegen")

/*
bable 的ast节点转为 es  的节点
 */
function bableAstToEsAst(bableAst, isRoot){
    var code = babel_tool.genCodeByAst(bableAst);
    // console.log("bable:",code);
    var esAst = es_tool.getAstByString(code);
    // var c = escodegen.generate(esAst.body[0]);
    if(isRoot){
        return esAst;
    }
    return esAst.body[0];
}

/*
es 的ast节点 转为 bable 的ast节点
 */
function esAstToBableAst(esAst){
    var code = es_tool.getCodeByAst(esAst);
    var bableAst = babel_tool.getAstByString(code);
    return bableAst;
}

/*
使用es 遍历出所有函数并修改它的参数名
 */
function renameAllFuncParams(ast, names, repeate_vars){
    var babelNode = esAstToBableAst(ast);
    babel_tool.renameAllFuncParam(babelNode, names, repeate_vars);
    return bableAstToEsAst(babelNode, true);
}

function renameAllCacheParam(ast, names, repeate_vars){
    var babelNode = esAstToBableAst(ast);
    babel_tool.renameAllCacheParam(babelNode, names, repeate_vars);
    return bableAstToEsAst(babelNode, true);
}

function renameAllFuncName(ast, names, repeate_vars){
    var babelNode = esAstToBableAst(ast);
    babel_tool.renameAllFuncName(babelNode, names, repeate_vars);
    return bableAstToEsAst(babelNode, true);
}

/*
重命名所有的变量名
 */
function renameAllVar(ast, names, repeate_vars){
    var babelNode = esAstToBableAst(ast);
    babel_tool.renameAllVar(babelNode, names, repeate_vars);
    return bableAstToEsAst(babelNode, true);
}



module.exports = {
    bableAstToEsAst,
    esAstToBableAst,
    renameAllFuncParams,
    renameAllVar,
    renameAllFuncName,
    renameAllCacheParam
}